package com.darren.euc.action.app;

import com.darren.euc.entity.Member;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.service.MsgService;
import com.darren.euc.util.Kv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by JUECHENG at 2018/3/17 11:45.
 */
@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/_msg")
public class _MsgAction {

    @Autowired
    private MsgService msgService;

    /**
     * 消息列表
     */
    @ResponseBody
    @RequestMapping(value = "/msg_list")
    public ResponseJSON msgList(HttpServletRequest request,
                                @RequestParam(name = "pn",required = false, defaultValue = "1")int pn,
                                @RequestParam(name = "ps",required = false, defaultValue = "10")int ps){
        ResponseJSON bean = new ResponseJSON();
        Member member = (Member) request.getAttribute("member");
        Kv kv = Kv.of();
        if (member != null){
            kv.set("member_id", member.getId());
            kv.set("mobile", member.getMobile());
            if (member.getSpaceId() != null){
                kv.set("space_id", member.getSpaceId());
                kv.set("client_id",member.getClientId());
            }
        }

        PageBean list = msgService.msgListV2(kv, pn, ps);
        return bean.setData(list);
    }

    /**
     * 消息详情
     * @throws UnsupportedEncodingException 
     */
    @RequestMapping(value = "/msg_detail")
    public void msgDetail(HttpServletRequest request,
                          @RequestParam(name = "message_id") int message_id,
                          Model model) throws UnsupportedEncodingException{
        Member member = (Member) request.getAttribute("member");

        Map map = msgService.msgInfo(message_id);
        System.out.println("==================");
        System.out.println(map.get("content"));
       // map.put("content", java.net.URLDecoder.decode( ,"UTF-8"));
        String content = (String) map.get("content");
        String enc = "UTF-8";
        map.put("content",java.net.URLDecoder.decode(content, enc));
        model.addAttribute("bean", map);

        //记录阅读记录
        if (member != null){
            msgService.msgLogSave(member.getId(), message_id);
        }
    }

    /**
     * 消息统计
     */
    @ResponseBody
    @RequestMapping(value = "/msg_count")
    public ResponseJSON msgCount(HttpServletRequest request){
        ResponseJSON bean = new ResponseJSON();
        Member member = (Member) request.getAttribute("member");

        //未登录直接返回
        if (member == null){
            return bean.setData(Kv.of("count", 0));
        }

        Kv kv = Kv.of();
        kv.set("member_id", member.getId());
        kv.set("mobile", member.getMobile());
        if (member.getSpaceId() != null){
            kv.set("space_id", member.getSpaceId());
            kv.set("client_id",member.getClientId());
        }
        Map map = msgService.msgCountV2(kv);
        return bean.setData(map);
    }

    /**
     * 一键全看
     */
    @ResponseBody
    @RequestMapping(value = "/readed_all")
    public ResponseJSON readedAll(HttpServletRequest request){
        Member member = (Member) request.getAttribute("member");

        //未登录直接返回
        if (member == null){
            return new ResponseJSON(-3,"未登陆",null);
        }

        Kv kv = Kv.of();
        kv.set("member_id", member.getId());
        kv.set("mobile", member.getMobile());
        if (member.getSpaceId() != null){
            kv.set("space_id", member.getSpaceId());
            kv.set("client_id",member.getClientId());
        }
        msgService.readedAllV2(kv);
        return new ResponseJSON();
    }

}
